﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.ETL.Domain.DataVerification;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;

namespace CPPEI.ETL.Domain.Services
{
    public class DataService
    {
        /// <summary>
        /// 验证数据的格式
        /// </summary>
        /// <param name="serData"></param>
        /// <param name="dtColumns"></param>
        /// <param name="passRows"></param>
        /// <param name="unpassRows"></param>
        public static void ValidateDataType(IData serData, List<Views.VDtTableColumn> dtColumns, ref List<Row> passRows, ref List<Row> unpassRows)
        {
            int rowCount = serData.RowCount;
            for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)
            {
                Row serRow = serData.Get(rowIndex);

                bool isPassed = true;
                // check
                for (int colIndex = 0; colIndex < dtColumns.Count; colIndex++)
                {
                    Views.VDtTableColumn vDtColumn = dtColumns[colIndex];

                    if (string.IsNullOrEmpty(vDtColumn.MappingColumnName))
                    {
                        // 没有映射数据到数据源的列，则跳过验证，因为这个信息不会插入到数据表
                        continue;
                    }

                    string value = serRow.Get(vDtColumn.MappingColumnName);


                    string changedValue = null;
                    object dbValue = null;
                    if (!TaskRuntime.RTParameter.Validate4DBAndChange(vDtColumn.Nullable, vDtColumn.Type, vDtColumn.Length, value, out dbValue, out changedValue))
                    {
                        // 修改一下数据，不然忘缓存表插入也会失败
                        Logger.DebugToTag(Library.LogTAG, string.Format("将{0}处理为{1}", value, changedValue));
                        serRow.Set(vDtColumn.MappingColumnName, changedValue);
                        isPassed = false;
                    }
                    else
                    {
                        // 将row里的数据修改成数据库需要的类型的新值
                        serRow.Set(vDtColumn.MappingColumnName, dbValue);
                    }
                }

                if(isPassed)
                {
                    passRows.Add(serRow);
                }
                else
                {
                    unpassRows.Add(serRow);
                }
            }
        }

        public static void ValidateDVR(EtlDbContext dbContext,ref List<Row> serRows, List<Entities.EDataVerificationRule> rules, ref List<Row> unpassRows)
        {
            List<IDataVerification> dataVerifications = new List<IDataVerification>();

            for (int dvrIndex = 0; dvrIndex < rules.Count; dvrIndex++)
            {
                Entities.EDataVerificationRule rule = rules[dvrIndex];
                if (rule.Type == DEF.DataVerificationRuleType.Script)
                {
                    dataVerifications.Add(new ScriptDataVerification(rule.Condition));
                }
                else if (rule.Type == DEF.DataVerificationRuleType.StoredProcedure)
                {
                    IDataVerification dataVerification = new SPDataVerification(rule.Condition);

                    EntityRepository<Entities.EDVRParameter> dvrpRep = new EntityRepository<ENT.EDVRParameter>(dbContext);
                    List<Entities.EDVRParameter> parameters = dvrpRep.GetList(p => p.DVRId == rules[dvrIndex].Id);
                    Parameter[] parameterArr = new Parameter[parameters.Count];
                    for (int index = 0; index < parameters.Count; index++)
                    {
                        Parameter param = new Parameter();
                        param.Name = parameters[index].Name;
                        param.Value = parameters[index].Value;
                        parameterArr[index] = param;
                    }
                    (dataVerification as IParameterize).Parameters = parameterArr;
                    dataVerifications.Add(dataVerification);
                }
                else if (rule.Type == DEF.DataVerificationRuleType.PlugIn)
                {
                    string[] plugInArr = rule.Condition.Split(new char[] { ',' });
                    IDataVerification dataVerification = PlugInManager.Create(plugInArr[0], plugInArr[1]);
                    if (dataVerification != null)
                    {
                        dataVerifications.Add(dataVerification);
                    }
                }
            }

            for (int index = 0; index < dataVerifications.Count; index++)
            {
                if(dataVerifications[index] is IDataVerificationContext)
                {
                    (dataVerifications[index] as IDataVerificationContext).Open();
                }
            }

            // 逐行验证
            for (int indexRow = 0; indexRow < serRows.Count; )
            {
                Row row = serRows[indexRow];

                bool isPass = true;
                for (int indexRule = 0; indexRule < dataVerifications.Count; indexRule++)
                {
                    if(!dataVerifications[indexRule].Verify(row))
                    {
                        unpassRows.Add(row);
                        isPass = false;
                        break;
                    }
                }

                if(!isPass)
                {
                    // remove unpassed row
                    serRows.RemoveAt(indexRow);
                }
                else
                {
                    indexRow++;
                }
            }

            for (int index = 0; index < dataVerifications.Count; index++)
            {
                if (dataVerifications[index] is IDataVerificationContext)
                {
                    (dataVerifications[index] as IDataVerificationContext).Close();
                }
            }
        }


        /// <summary>
        /// 编码转换
        /// </summary>
        /// <param name="serRows"></param>
        /// <param name="?"></param>
        /// <param name="dtColumns"></param>
        /// <param name="rules"></param>
        /// <param name="unpassRows"></param>
        public static void ValidateCCR(ref List<Row> serRows, List<Views.VDtTableColumn> dtColumns, List<Views.VCodeConversionRule> rules, ref List<Row> unpassRows)
        {
            for (int rowIndex = 0; rowIndex < serRows.Count;)
            {
                Row serRow = serRows[rowIndex];
                bool isPass = true;
                for (int dtColIndex = 0; dtColIndex < dtColumns.Count; dtColIndex++)
                {
                    Views.VDtTableColumn dtColumn = dtColumns[dtColIndex];

                    string code = serRow.Get(dtColumn.MappingColumnName);

                    bool isReplaced = false;
                    bool needReplace = false;
                    #region // 获取rule
                    for (int ruleIndex = 0; ruleIndex < rules.Count; ruleIndex++)
                    {
                        Views.VCodeConversionRule rule = rules[ruleIndex];
                        if(rule.DtColumnId == dtColumn.Id)
                        {
                            needReplace = true;
                            string[] rawCodeArr = rule.RawCodes.Split(new char[] { ',' });

                            for (int rawCodeIndex = 0; rawCodeIndex < rawCodeArr.Length; rawCodeIndex++)
                            {
                                if(string.Equals(rawCodeArr[rawCodeIndex], code))
                                {
                                    Row.Set(serRow, dtColumn.MappingColumnName, rule.DimItemCode);

                                    isReplaced = true;
                                    break;
                                }
                            }
                            break;
                        }
                    }
                    #endregion

                    if (needReplace && !isReplaced)
                    {
                        // 出错
                        isPass = false;
                        break;
                    }
                }

                if(isPass)
                {
                    rowIndex++;
                }
                else
                {
                    serRows.RemoveAt(rowIndex);
                    unpassRows.Add(serRow);
                }
            }
        }

        /// <summary>
        /// 区分表是否是原始数据表进行不同的插入
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="batchId"></param>
        /// <param name="createdTime"></param>
        /// <param name="rows"></param>
        /// <param name="eDtTable"></param>
        /// <param name="vDtColumns"></param>
        /// <param name="dataIdentities"></param>
        /// <param name="unpassRows"></param>
        public static void InsertToTable(EtlDbContext dbContext,
            Guid batchId,
            DateTime createdTime,
            List<Row> rows,
            Entities.EDtTable eDtTable,
            List<Views.VDtTableColumn> vDtColumns,
            List<Entities.EDtSourceDataIdentity> dataIdentities,
            ref List<Row> unpassRows)
        {
            if (eDtTable.IsRawDt)
            {
                InsertToRawTable(dbContext, batchId, createdTime, rows, eDtTable, vDtColumns, dataIdentities, ref unpassRows);
            }
            else
            {
                InsertToThirdPartTable(dbContext, batchId, createdTime, rows, eDtTable, vDtColumns, dataIdentities, ref unpassRows);
            }
        }

        /// <summary>
        /// 
        /// 插入数据
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="batchId"></param>
        /// <param name="createdTime"></param>
        /// <param name="rows"></param>
        /// <param name="eDtTable"></param>
        /// <param name="vDtColumns"></param>
        /// <param name="dataIdentities">数据表示列信息</param>
        /// <param name="unpassRows"></param>
        public static void InsertToRawTable(EtlDbContext dbContext,
            Guid batchId,
            DateTime createdTime,
            List<Row> rows,
            Entities.EDtTable eDtTable,
            List<Views.VDtTableColumn> vDtColumns,
            List<Entities.EDtSourceDataIdentity> dataIdentities,
            ref List<Row> unpassRows)
        {
            List<string> colNames = new List<string>();
            for (int index = 0; index < vDtColumns.Count; index++)
			{
			    colNames.Add(vDtColumns[index].Name);
            }

            List<string> uniqueColumns = new List<string>();
            for (int index = 0; index < dataIdentities.Count; index++)
            {
                for (int colIndex = 0; colIndex < vDtColumns.Count; colIndex++)
                {
                    if (dataIdentities[index].DtSourceColumnId == vDtColumns[colIndex].MappingColumnId)
                    {
                        uniqueColumns.Add(vDtColumns[colIndex].Name);
                        break;
                    }
                }
            }

            for (int rowIndex = 0; rowIndex < rows.Count; )
            {
                Row row = rows[rowIndex];
                List<object> values = new List<object>();

                for (int dtColIndex = 0; dtColIndex < vDtColumns.Count; dtColIndex++)
                {
                    string value = row.Get(vDtColumns[dtColIndex].MappingColumnName);
                    if (vDtColumns[dtColIndex].Nullable && string.IsNullOrEmpty(value))
                    {
                        values.Add(null);
                    }
                    else
                    {
                        values.Add(value);
                    }
                }

                if (CPPEI.ETL.Domain.Util.DtTableUtil.InsertToRawTable(dbContext, 
                    eDtTable.ActualName,
                    Guid.NewGuid(),
                    batchId,
                    createdTime,
                    row.RowIndex,
                    colNames, 
                    values,
                    uniqueColumns))
                {
                    // pass 
                    rowIndex++;
                }
                else
                {
                    rows.RemoveAt(rowIndex);
                    unpassRows.Add(row);
                }
            }
        }


        public static void InsertToRawTableNoTry(Guid batchId,
            DateTime createdTime,
            List<Row> rows,
            Entities.EDtTable eDtTable,
            List<Views.VDtTableColumn> vDtColumns,
            List<Entities.EDtSourceDataIdentity> dataIdentities,
            ref List<Row> unpassRows)
        {
            using (SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["etl-db"].ConnectionString))
            {
                connection.Open();
                List<string> colNames = new List<string>();
                for (int index = 0; index < vDtColumns.Count; index++)
                {
                    colNames.Add(vDtColumns[index].Name);
                }

                List<string> uniqueColumns = new List<string>();
                for (int index = 0; index < dataIdentities.Count; index++)
                {
                    for (int colIndex = 0; colIndex < vDtColumns.Count; colIndex++)
                    {
                        if (dataIdentities[index].DtSourceColumnId == vDtColumns[colIndex].MappingColumnId)
                        {
                            uniqueColumns.Add(vDtColumns[colIndex].Name);
                            break;
                        }
                    }
                }

                for (int rowIndex = 0; rowIndex < rows.Count; )
                {
                    Row row = rows[rowIndex];
                    List<object> values = new List<object>();

                    for (int dtColIndex = 0; dtColIndex < vDtColumns.Count; dtColIndex++)
                    {
                        string value = row.Get(vDtColumns[dtColIndex].MappingColumnName);
                        if (vDtColumns[dtColIndex].Nullable && string.IsNullOrEmpty(value))
                        {
                            values.Add(null);
                        }
                        else
                        {
                            values.Add(value);
                        }
                    }

                    if (CPPEI.ETL.Domain.Util.DtTableUtil.InsertToRawTable(connection,
                        eDtTable.ActualName,
                        Guid.NewGuid(),
                        batchId,
                        createdTime,
                        row.RowIndex,
                        colNames,
                        values,
                        uniqueColumns))
                    {
                        // pass 
                        rowIndex++;
                    }
                    else
                    {
                        rows.RemoveAt(rowIndex);
                        unpassRows.Add(row);
                    }
                }
            }
        }

        /// <summary>
        /// 插入数据(插入到普通的数据表)
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="batchId"></param>
        /// <param name="createdTime"></param>
        /// <param name="rows"></param>
        /// <param name="eDtTable"></param>
        /// <param name="vDtColumns"></param>
        /// <param name="dataIdentities">数据表示列信息</param>
        /// <param name="unpassRows"></param>
        public static void InsertToThirdPartTable(EtlDbContext dbContext,
            Guid batchId,
            DateTime createdTime,
            List<Row> rows,
            Entities.EDtTable eDtTable,
            List<Views.VDtTableColumn> vDtColumns,
            List<Entities.EDtSourceDataIdentity> dataIdentities,
            ref List<Row> unpassRows)
        {
            List<string> uniqueColumns = new List<string>();
            for (int index = 0; index < dataIdentities.Count; index++)
            {
                for (int colIndex = 0; colIndex < vDtColumns.Count; colIndex++)
                {
                    if (dataIdentities[index].DtSourceColumnId == vDtColumns[colIndex].MappingColumnId)
                    {
                        uniqueColumns.Add(vDtColumns[colIndex].Name);
                        break;
                    }
                }
            }

            for (int rowIndex = 0; rowIndex < rows.Count; )
            {
                Row row = rows[rowIndex];
                List<object> values = new List<object>();
                List<string> colNames = new List<string>();

                for (int dtColIndex = 0; dtColIndex < vDtColumns.Count; dtColIndex++)
                {
                    if (!string.IsNullOrEmpty(vDtColumns[dtColIndex].MappingColumnName))
                    {
                        string value = row.Get(vDtColumns[dtColIndex].MappingColumnName);

                        values.Add(value);
                        colNames.Add(vDtColumns[dtColIndex].Name);
                    }
                }

                if (CPPEI.ETL.Domain.Util.DtTableUtil.InsertToTable(dbContext,
                    eDtTable.ActualName,
                    Guid.NewGuid(),
                    batchId,
                    createdTime,
                    row.RowIndex,
                    colNames,
                    values,
                    uniqueColumns))
                {
                    // pass 
                    rowIndex++;
                }
                else
                {
                    rows.RemoveAt(rowIndex);
                    unpassRows.Add(row);
                }
            }
        }

        public static void InsertToRawTableFromCache(EtlDbContext dbContext,
            Guid batchId,
            DateTime createdTime,
            List<Row> rowsInCache,
            Entities.EDtTable eDtTable,
            List<Views.VDtTableColumn> vDtColumns,
            List<Entities.EDtSourceDataIdentity> dataIdentities,
            ref List<Row> unpassRows)
        {
            List<string> colNames = new List<string>();
            for (int index = 0; index < vDtColumns.Count; index++)
            {
                colNames.Add(vDtColumns[index].Name);
            }

            List<string> uniqueColumns = new List<string>();
            for (int index = 0; index < dataIdentities.Count; index++)
            {
                for (int colIndex = 0; colIndex < vDtColumns.Count; colIndex++)
                {
                    if (dataIdentities[index].DtSourceColumnId == vDtColumns[colIndex].MappingColumnId)
                    {
                        uniqueColumns.Add(vDtColumns[colIndex].Name);
                        break;
                    }
                }
            }

            for (int rowIndex = 0; rowIndex < rowsInCache.Count; )
            {
                Row row = rowsInCache[rowIndex];
                List<object> values = new List<object>();

                for (int dtColIndex = 0; dtColIndex < vDtColumns.Count; dtColIndex++)
                {
                    string value = row.Get(vDtColumns[dtColIndex].Name);
                    if(string.IsNullOrEmpty(value) && vDtColumns[dtColIndex].Nullable)
                    {
                        values.Add(null);
                    }
                    else
                    {
                        values.Add(value);
                    }
                }

                if (CPPEI.ETL.Domain.Util.DtTableUtil.InsertToRawTable(dbContext,
                    eDtTable.ActualName,
                    Guid.NewGuid(),
                    batchId,
                    createdTime,
                    row.RowIndex,
                    colNames,
                    values,
                    uniqueColumns))
                {
                    // pass 
                    rowIndex++;
                }
                else
                {
                    rowsInCache.RemoveAt(rowIndex);
                    unpassRows.Add(row);
                }
            }
        }

        public static void InsertToCache(EtlDbContext dbContext,
            Guid batchId,
            DateTime createdTime,
            int state,
            List<Row> rows, 
            Entities.EDtTable eDtTable,
            List<Views.VDtTableColumn> vDtColumns)
        {
            List<string> colNames = new List<string>();
            for (int index = 0; index < vDtColumns.Count; index++)
            {
                colNames.Add(vDtColumns[index].Name);
            }

            for (int rowIndex = 0; rowIndex < rows.Count; rowIndex++)
            {
                Row row = rows[rowIndex];
                List<object> values = new List<object>();

                for (int dtColIndex = 0; dtColIndex < vDtColumns.Count; dtColIndex++)
                {
                    string value = row.Get(vDtColumns[dtColIndex].MappingColumnName);
                    values.Add(value);
                }

                if (!CPPEI.ETL.Domain.Util.DtTableUtil.InsertToCacheTable(dbContext,
                    eDtTable.CacheName,
                    Guid.NewGuid(),
                    batchId,
                    row.RowIndex,
                    state,
                    createdTime,
                    colNames,
                    values))
                {
                    Logger.ErrorToTag(Library.LogTAG, string.Format("插入数据出错,table:{0}", eDtTable.CacheName));
                }
            }
        }


        public static void UpdateToCache(EtlDbContext dbContext,
            Guid batchId,
            DateTime createdTime,
            int state,
            Entities.EDtTable eDtTable,
            List<Row> rows)
        {
            for (int index = 0; index < rows.Count; index++)
            {
                Guid dataId = new Guid(rows[index].Get(DEF.DtTableColumns.SYSCOL_Id));
                CPPEI.ETL.Domain.Util.DtTableUtil.UpdateToCacheTable(dbContext,
                    eDtTable.CacheName,
                    batchId,
                    dataId,
                    createdTime,
                    state);
            }
        }

        public static List<Row> GetFromCache(EtlDbContext dbContext,
            Guid batchId,
            int state,
            Entities.EDtTable eDtTable,
            List<Views.VDtTableColumn> vDtColumns)
        {
            List<string> colNames = new List<string>();
            for (int index = 0; index < vDtColumns.Count; index++)
            {
                colNames.Add(vDtColumns[index].Name);
            }
            colNames.Add(DEF.DtTableColumns.SYSCOL_RowIndex);

            System.Data.DataTable dataTable = CPPEI.ETL.Domain.Util.DtTableUtil.GetFromCacheTable(dbContext,
                    eDtTable.CacheName, batchId, state, colNames);
            if(dataTable == null)
            {
                return null;
            }
            else
            {
                List<Row> rows = new List<Row>();
                for (int rowIndex = 0; rowIndex < dataTable.Rows.Count; rowIndex++)
                {
                    System.Data.DataRow dataRow = dataTable.Rows[rowIndex];
                    Row row = new Row(Convert.ToInt32(dataRow[DEF.DtTableColumns.SYSCOL_RowIndex]));
                    for (int index = 0; index < vDtColumns.Count; index++)
                    {
                        Row.Add(row, vDtColumns[index].Name, Convert.ToString(dataRow[vDtColumns[index].Name]));
                    }
                    rows.Add(row);
                }
                return rows;
            }
        }

        public static void GetFromCache(EtlDbContext dbContext,
            Guid batchId,
            int state,
            Entities.EDtTable eDtTable,
            List<Views.VDtTableColumn> vDtColumns,
            List<Guid> passIdList,
            List<Guid> unpassIdList,
            ref List<Row> passRows,
            ref List<Row> unpassRows)
        {
            List<string> colNames = new List<string>();
            for (int index = 0; index < vDtColumns.Count; index++)
            {
                colNames.Add(vDtColumns[index].Name);
            }
            colNames.Add(DEF.DtTableColumns.SYSCOL_Id);
            colNames.Add(DEF.DtTableColumns.SYSCOL_RowIndex);

            System.Data.DataTable dataTable = CPPEI.ETL.Domain.Util.DtTableUtil.GetFromCacheTable(dbContext,
                    eDtTable.CacheName, batchId, state, colNames);

            if (dataTable != null)
            {
                for (int rowIndex = 0; rowIndex < dataTable.Rows.Count; rowIndex++)
                {
                    if (passIdList.Count == 0 && unpassIdList.Count == 0)
                    {
                        break;
                    }

                    System.Data.DataRow dataRow = dataTable.Rows[rowIndex];

                    Guid dataId = new Guid(Convert.ToString(dataRow[DEF.DtTableColumns.SYSCOL_Id]));

                    bool isExist = false;
                    for (int idIndex = 0; idIndex < passIdList.Count; idIndex++)
                    {
                        if (dataId == passIdList[idIndex])
                        {
                            isExist = true;
                            passIdList.RemoveAt(idIndex);

                            Row row = new Row(Convert.ToInt32(dataRow[DEF.DtTableColumns.SYSCOL_RowIndex]));
                            for (int index = 0; index < colNames.Count; index++)
                            {
                                Row.Add(row, colNames[index], Convert.ToString(dataRow[colNames[index]]));
                            }
                            passRows.Add(row);
                            break;
                        }
                    }

                    if (isExist)
                    {
                        continue;
                    }

                    for (int idIndex = 0; idIndex < unpassIdList.Count; idIndex++)
                    {
                        if (dataId == unpassIdList[idIndex])
                        {
                            isExist = true;
                            unpassIdList.RemoveAt(idIndex);

                            Row row = new Row(Convert.ToInt32(dataRow[DEF.DtTableColumns.SYSCOL_RowIndex]));
                            for (int index = 0; index < colNames.Count; index++)
                            {
                                Row.Add(row, colNames[index], Convert.ToString(dataRow[colNames[index]]));
                            }
                            unpassRows.Add(row);
                            break;
                        }
                    }

                }
            }
        }

        public ServiceResult<Models.MDtTableData> GetDataList(Guid taskId, int start, int limit)
        {
            using(EtlDbContext dbContext  = new EtlDbContext())
            {
                EntityRepository<Entities.ETask> tRep = new EntityRepository<ENT.ETask>(dbContext);
                Entities.ETask eTask = tRep.Get(p => p.Id == taskId);
                if(eTask == null)
                {
                    return new ServiceResult<Models.MDtTableData>(false, ErrorCode.COM_OBJNotExist, ErrorCode.String(ErrorCode.COM_OBJNotExist));
                }
                
                EntityRepository<Entities.ETaskExecuteRecord> terRep = new EntityRepository<ENT.ETaskExecuteRecord>(dbContext);
                Entities.ETaskExecuteRecord eTaskExecuteRecord = terRep.Query(p => p.TaskId == taskId && p.IsFinished == false).FirstOrDefault();
                if (eTaskExecuteRecord == null)
                {
                    // 任务已经完成，不存在需要审核的数据
                    return new ServiceResult<Models.MDtTableData>(false, ErrorCode.TaskNoUnfinishedRecord, ErrorCode.String(ErrorCode.TaskNoUnfinishedRecord));
                }

                Guid batchId = eTaskExecuteRecord.BatchId;

                EntityRepository<Entities.EDtSourceMapping> dsmRep = new EntityRepository<Entities.EDtSourceMapping>(dbContext);
                Entities.EDtSourceMapping eDtSourceMapping = dsmRep.Get(p => p.DtSerId == eTask.DtSerId && p.DtSourceId == eTask.DtSourceId);
                if (eDtSourceMapping == null)
                {
                    return new ServiceResult<Models.MDtTableData>(false, ErrorCode.NoMappintDtTable, ErrorCode.String(ErrorCode.NoMappintDtTable));
                }
                
                EntityRepository<Entities.EDtTable> dtRep = new EntityRepository<Entities.EDtTable>(dbContext);
                
                Guid dtTableId = dsmRep.Get(p => p.DtSerId == eTask.DtSerId && p.DtSourceId == eTask.DtSourceId).DtTableId;
                Entities.EDtTable eTable = dtRep.Get(p => p.Id == dtTableId);

                EntityRepository<Entities.EDtTableColumn> dtcRep = new EntityRepository<Entities.EDtTableColumn>(dbContext);

                List<Entities.EDtTableColumn> vDtColumns = dtcRep.GetList(p => p.DtTableId == eTable.Id);

                List<string> columns = new List<string>();
                for (int index = 0; index < vDtColumns.Count; index++)
                {
                    Entities.EDtTableColumn vDtColumn = vDtColumns[index];
                    columns.Add(vDtColumn.Name);
                }

                int total = 0;
                System.Data.DataTable dataTable = CPPEI.ETL.Domain.Util.DtTableUtil.GetPagedDataFromCacheTable(dbContext, 
                    eTable.CacheName,
                    batchId,
                    DEF.RawDataStatus.WaitDataVerification, 
                    columns,
                    start, 
                    limit,
                    out total);

                Models.MDtTableData mDtTableData = new MOD.MDtTableData(start, limit, total, dataTable);

                return new ServiceResult<Models.MDtTableData>(true, ErrorCode.COM_NoError, ErrorCode.String(ErrorCode.COM_NoError), mDtTableData);
            }
        }


        internal static void InsertToCacheNoTry(EtlDbContext dbContext,
            Guid batchId,
            DateTime createdTime,
            int state,
            List<Row> rows,
            Entities.EDtTable eDtTable,
            List<Views.VDtTableColumn> vDtColumns)
        {
            List<string> colNames = new List<string>();
            for (int index = 0; index < vDtColumns.Count; index++)
            {
                colNames.Add(vDtColumns[index].Name);
            }

            for (int rowIndex = 0; rowIndex < rows.Count; rowIndex++)
            {
                Row row = rows[rowIndex];
                List<object> values = new List<object>();

                for (int dtColIndex = 0; dtColIndex < vDtColumns.Count; dtColIndex++)
                {
                    string value = row.Get(vDtColumns[dtColIndex].MappingColumnName);
                    values.Add(value);
                }

                CPPEI.ETL.Domain.Util.DtTableUtil.InsertToCacheTableNoTry(dbContext,
                    eDtTable.CacheName,
                    Guid.NewGuid(),
                    batchId,
                    row.RowIndex,
                    state,
                    createdTime,
                    colNames,
                    values);
            }
        }
    }
}
